package com.example.smartcity_poverty.ui.PersonalCenter.activity

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.network.AppApi
import com.example.smartcity_poverty.logic.network.ServiceNET.Companion.create
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_login_info.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject

class LoginInfoActivity : AppCompatActivity() {

    private lateinit var phone1: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_login_info)
        title = "个人信息"

        // 修改个人信息
        loginInfoOk.setOnClickListener {
            setOk()
        }

        // 当用户修改密码时，点击显示
        loginInfoPhone.setOnClickListener {
            loginInfoPhone.setText(phone1)
        }
    }

    private fun setOk() {
        getSharedPreferences("token", Context.MODE_PRIVATE).apply {
            val token = this.getString("token", "").toString()
            GlobalScope.launch {
                create<AppApi>().newLoginInfo(token, JSONObject().apply {
                    put("nickName", loginInfoName.text.toString())
                    if (radio1.isChecked) put("sex", "0")
                    else put("sex", "1")
                    put("phonenumber", loginInfoPhone.text.toString())
                }.toString().toRequestBody("application/json".toMediaTypeOrNull())).apply {
                    if (this.code == "200"){
                        runOnUiThread {
                            this.msg.show()
                        }
                    }else {
                        runOnUiThread { this.msg.show() }
                    }
                }
            }
        }

    }


    override fun onStart() {
        super.onStart()
        getSharedPreferences("token", Context.MODE_PRIVATE).apply {
            val token = this.getString("token", "").toString()
            GlobalScope.launch(Dispatchers.IO){
                create<AppApi>().loginAll(token).apply {
                    if (this.code == 200){
                        runOnUiThread {
                            loginInfoName.setText(this.user?.nickName)
                            if (this.user?.sex == "0") radio1.isChecked = true
                            if (this.user?.sex == "1") radio2.isChecked = true
                            phone1 = this.user?.phonenumber.toString()
                            val phone = this.user?.phonenumber?.substring(0,8) + "****"
                            loginInfoPhone.setText(phone)
                        }
                    }
                }
            }
        }

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}